define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');

	var List = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-product',
				'urlDelete': 'condition/remove',
				'view_model': {
					'view': {
						'addButton': pubjs.checkRight('condition.add')
					}
				}
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			// var c = self.getConfig();

			self.append($([
				'<div class="M-formHead">',
					'<span ms-visible="view.addButton" class="ml10 addButton" pub-name="addButton" pub-mod="@base/common/drop.add"></span>',
					'<span class="fr" pub-name="date" pub-mod="@base/common/date.dateRangeNew"/>',
				'</div>',
				'<div class="cl" pub-name="highGrid" pub-mod="condition/grid/list.base"/>'
			].join('')));
			self.buildFromTemplate({
				'highGrid': {
					'hasMenu': true
				},
				'date': {
					'pos': 'bR'
				},
				'addButton': {
					'def': LANG('新建条件'),
					'href': null,
					'useCallback': true
				}
			});

			self.createAsync('editPop', 'promotion/popwin/popwin.condition', {
				'autoShow': false
			});
		},
		// 操作列点击事件
		onOperateMenuShow: function( ev ) {
			var param = ev.param;
			var result = [];
			if (pubjs.checkRight('condition.update')) {
				result.push({'id': 'edit', 'name': LANG('编辑')});
			}

			if (pubjs.checkRight('condition.remove')) {
				result.push({'id': 'delete', 'name': LANG('删除')});
			}

			ev.returnValue = result;
			return false;
		},
		// 操作列选项点击事件
		onOperateMenuSelect: function( ev ) {
			var key = ev.param[0]['key'];
			var id = ev.param[1]['_id'];
			var name = ev.param[1]['Name'];
			var self = this;
			var c = self.getConfig();
			switch(key){
				case 'edit':
					self.editCondition(id);
				break;
				case 'delete':
					pubjs.confirm(LANG('确认删除条件 %1 吗', name), function(state) {
						if(state)
						{
							pubjs.loading.show();
							pubjs.mc.send(c.urlDelete, {'_id': +id}, function(err) {
								pubjs.loading.hide();
								if(err)
								{
									return pubjs.alert(err.message);
								}

								pubjs.alert(LANG('删除成功'));
								self.reload();
							});
						}
					});
				break;
			}
		},
		onInputClick: function(ev){
			var self = this;
			if(ev.name == 'addButton'){
				if(self.get('editPop')){
					self.get('editPop').reset('buildOne').show().load();
				}
			}
		},
		editCondition: function(id){
			var self = this;
			if(self.get('editPop')){
				self.get('editPop').reset().show().load(id);
			}
		},
		onUpdateCondition: function(ev){
			this.reload();
			this.$currentId = ev.param._id;
			this.$.highGrid.setRowHighlight(ev.param._id);
		},
		reload: function( param ) {
			var grid = this.$.highGrid;
			grid.unsetRowHighlight(this.$currentId);
			grid.reload();
			return this;
		},
		onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;
				switch (type) {
				}
			});

			self.$.highGrid.reload(param);
		}
	});

	exports.base = List;
});